$(function () {
    const body = $('body');

    function hideCodeButtons() {
        $('#field-code-btn').addClass('is-hidden')
        $('#field-password').removeClass('is-hidden')
        $('#btn-login').removeClass('is-static')
    }

    body.on('click', '#btn-send-code', function (event) {
        if (isNotValidForm('form-login', ['password'])) {
            return
        }
        const data = {}
        data['username'] = $('form#form-login input[name=username]').val().trim()

        const otpParam = {}
        otpParam['path'] = '/api/auth/otp'
        otpParam['method'] = 'post'
        otpParam['data'] = data
        loadButtonClick(event, otpParam, () => {
            $('#btn-enter-code').addClass('is-static')
            hideCodeButtons();
        })
    });

    body.on('click', '#btn-enter-code', function () {
        hideCodeButtons()
    });

    body.on('click', '#btn-login', function (event) {
        if (isNotValidForm('form-login', [])) {
            return
        }
        const data = {}
        data['username'] = $('form#form-login input[name=username]').val().trim()
        data['password'] = $('form#form-login input[name=password]').val().trim()

        const loginParam = {}
        loginParam['path'] = '/api/auth/login'
        loginParam['method'] = 'post'
        loginParam['data'] = data
        loginParam['contentType'] = 'application/x-www-form-urlencoded'
        loadButtonClick(event, loginParam, () => {
            window.location.replace("/dashboard")
        })
    });
})